<?php

class CaseServices extends Zend_Db_Table_Abstract {
	protected $_name = 'case_services';

	private $_errorCode = '';
	private $_errorStr = '';

	public function getErrorCode() {
		return $this->_errorCode;
	}
	public function getErrorStr() {
		return $this->_errorStr;
	}
	public function getErrorInfo() {
		return array ($this->_errorCode, $this->_errorStr );
	}

	public function getItemsForCase($FDMS_case_num) {
		if (strlen ( $FDMS_case_num ) < 1) {
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;
		}

		$items = array ();

		try {
			$rows = $this->fetchAll ( "FDMS_case_num='$FDMS_case_num'" );
			foreach ( $rows as $row )
			$items [] = $row->toArray ();
		} catch ( Exception $e ) {
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = '' . $e;
			$items = null;
		}

		return $items;
	}

	public function setItemsForCase(&$items, $FDMS_case_num, $replace = true) {
		if (strlen ( $FDMS_case_num ) < 1) {
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;
		}

		// first, delete all existing items for tihs case
		if ($replace)
		$this->delete ( "FDMS_case_num='$FDMS_case_num'" );

		// then insert the new ones
		try {
			$rowsInserted = 0;
			foreach ( $items as $item ) {
				$item ['FDMS_case_num'] = $FDMS_case_num;
				Zend_Debug::dump ( $item );
				$rowsInserted += $this->insert ( $item );
			}
		} catch ( Exception $e ) {
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = '' . $e;
			$items = null;
			Zend_Debug::dump ( $items );
			Zend_Debug::dump ( $e );
			exit ( 0 );
		}

		return $rowsInserted;
	}

	public function getServicesForLocaleId($locale_id) {

		$select = $this->getAdapter ()->select ()->from ( 'pricelist', array ('item_category' => 'itemCategory', 'line_number' => 'ContractLineNumber', 'item_description' => 'ContractDescription', 'price_list_id' => 'PriceListID', 'item_seq_number' => 'InvoiceSeqNo', 'item_amount' => 'Price' ) )->where ( "LocaleID=?", $locale_id );

		$result = $this->getAdapter ()->fetchAll ( $select );
		if ($result) {

			$rows = array ();
			foreach ( $result as $row ) {

				$itemnum = $row ['item_seq_number'];
				if ($itemnum >= 100 && $itemnum <= 199)
				$rows [] = $row;
			}
			return $rows;
		}
		return array ();
	}
}

?>